const app = getApp()
import axios from '../../utils/axios';

Page({

  data: {
    codeToast: "获取验证码",
    codeTime: 60,
    timeName: '',
    province: "", // 省区市名称
    postcode: "", // 邮政编码
    detailAddress: "", // 详细地址
    phoneNumber: "", // 手机号
    codeNumber: "", // 验证码
    userName: "", // 姓名
    auditShow: false,
  },

  onLoad: function (options) {

    console.log(app.globalData.userInfo.apply)
    if (!app.globalData.userInfo.apply || app.globalData.userInfo.apply == "false") {
      this.setData({
        auditShow: true
      })
    }

  },

  onShow: function () {

  },

  // 提交申请
  applyCommander: function () {

    if (this.data.province == "") {
      wx.showToast({
        title: '请选择地址！',
        icon: "none"
      })
      return
    }
    if (this.data.detailAddress == "") {
      wx.showToast({
        title: '请输入详细地址！',
        icon: "none"
      })
      return
    }
    if (this.data.phoneNumber.length != 11) {
      wx.showToast({
        title: '请输入正确手机号！',
        icon: "none"
      })
      return
    }
    if (this.data.codeNumber == "") {
      wx.showToast({
        title: '请输入验证码！',
        icon: "none"
      })
      return
    }
    if (this.data.userName == "") {
      wx.showToast({
        title: '请输入姓名！',
        icon: "none"
      })
      return
    }

    axios('/personal/applyCommander', {
      address: this.data.province,
      village: this.data.detailAddress,
      phone: this.data.phoneNumber,
      code: this.data.codeNumber,
      name: this.data.userName
    }).then(res => {
      if (res.data.resultcode == 1) {
        wx.showToast({
          title: '申请成功！',
          icon: "none"
        })
        this.setData({
          auditShow: true
        })
      }
    })

  },

  // 选择省市区
  selectProvince: function (e) {
    console.log(e)

    let province = ""
    let postcode = e.detail.postcode

    for (let i = 0; i < e.detail.value.length; i++) {
      province += e.detail.value[i]
    }

    this.setData({
      province: province,
      postcode: postcode
    })

  },

  // 详细地址输入事件
  inputDetail: function (e) {
    this.setData({
      detailAddress: e.detail.value
    })
  },

  // 手机号输入事件
  inputPhone: function (e) {
    this.setData({
      phoneNumber: e.detail.value
    })
  },

  // 验证码输入事件
  inputCode: function (e) {
    this.setData({
      codeNumber: e.detail.value
    })
  },

  // 姓名输入事件
  inputName: function (e) {
    this.setData({
      userName: e.detail.value
    })
  },

  // 获取验证码
  getCode: function (e) {

    if (this.data.phoneNumber.length != 11) {
      wx.showToast({
        title: '手机号长度不正确！',
        icon: "none"
      })
      return
    }

    if (this.data.codeTime < 60) {
      wx.showToast({
        title: '请稍候发送验证码！',
        icon: "none"
      })
      return
    }

    wx.showLoading({
      title: '发送中...',
      mask: true
    })

    const self = this

    axios('/personal/sendCode', {
      phone: self.data.phoneNumber
    }).then(res => {
      console.log(res)
      if (res.data.resultcode == 1) {

        let codeTime = self.data.codeTime

        self.setData({
          timeName: setInterval(() => {

            codeTime--
            console.log(codeTime)
            self.setData({
              codeToast: codeTime,
              codeTime: codeTime
            })
            if (codeTime <= 0) {
              self.setData({
                codeToast: "获取验证码",
                codeTime: 60
              })
              clearInterval(self.data.timeName)
            }

          }, 1000)

        })

      }
    })

  },

  // 点击返回首页
  backHome: function () {
    wx.navigateBack({
      delta: 1
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})